package article

import (
	"fmt"
	"gf-websocket/internal/dao"
	"gf-websocket/internal/model"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

func Home(r *ghttp.Request) {
	var articles []*model.Article

	err := dao.Article.Ctx(r.Context()).Scan(&articles)
	if err != nil {
		r.Response.Writeln(err)
		return
	}

	fmt.Println("articles:", articles)

	// 从请求中获取tag参数
	cid := r.GetQuery("cid").String()

	err = r.Response.WriteTpl("index.html", g.Map{
		"cid":     cid,      // 当前栏目名
		"artList": articles, // 当前栏目文章清单
	})
	if err != nil {
		r.Response.Writeln(err)
		return
	}
}

func Lists(r *ghttp.Request) {
	var articles []*model.Article

	err := dao.Article.Ctx(r.Context()).Scan(&articles)
	if err != nil {
		r.Response.Writeln(err)
		return
	}

	fmt.Println("articles:", articles)

	// 从请求中获取tag参数
	cid := r.GetQuery("cid").String()

	err = r.Response.WriteTpl("lists.html", g.Map{
		"cid":     cid,      // 当前栏目名
		"artList": articles, // 当前栏目文章清单
	})
	if err != nil {
		r.Response.Writeln(err)
		return
	}
}

// internal/controller/article/article.go
func Detail(r *ghttp.Request) {
	id := r.GetRouter("id").String()
	fmt.Printf("Requested article ID: %s\n", id) // 调试输出

	if id == "" {
		r.Response.WriteStatus(400, "Missing article ID")
		return
	}

	var articleDetail *model.Article
	err := dao.Article.Ctx(r.Context()).Where("id", id).Scan(&articleDetail)
	fmt.Printf("Query result - Error: %v, Article: %+v\n", err, articleDetail) // 调试输出

	// 渲染详情页模板
	err = r.Response.WriteTpl("detail.html", g.Map{
		"article": articleDetail,
	})
	if err != nil {
		r.Response.WriteStatus(500, err.Error())
	}
}

func Edit(r *ghttp.Request) {
	id := r.Get("id").Int()
	if id <= 0 {
		r.Response.WriteJson(g.Map{
			"code": 1,
			"msg":  "参数错误",
		})
		return
	}
	var article *model.Article
	err := dao.Article.Ctx(r.Context()).Where("id", id).Scan(&article)
	if err != nil {
		r.Response.WriteJson(g.Map{
			"code": 1,
			"msg":  "获取文章失败: " + err.Error(),
		})
		return
	}
	r.Response.WriteJson(g.Map{
		"code": 0,
		"msg":  "获取文章成功",
		"data": article,
	})

}

func Del(r *ghttp.Request) {
	id := r.Get("id").Int()
	if id <= 0 {
		r.Response.WriteJson(g.Map{
			"code": 1,
			"msg":  "参数错误",
		})
		return
	}
	_, err := dao.Article.Ctx(r.Context()).Where("id", id).Delete()
	if err != nil {
		r.Response.WriteJson(g.Map{
			"code": 1,
			"msg":  "删除失败: " + err.Error(),
		})
		return
	}
	r.Response.WriteJson(g.Map{
		"code": 0,
		"msg":  "删除成功",
	})
}

func Save(r *ghttp.Request) {
	id := r.Get("id").Int()
	if id <= 0 {
		r.Response.WriteJson(g.Map{
			"code": 1,
			"msg":  "参数错误",
		})
		return
	}
	title := r.Get("title").String()
	content := r.Get("content").String()
	if title == "" || content == "" {
		r.Response.WriteJson(
			g.Map{
				"code": 1,
				"msg":  "标题和内容不能为空",
			},
		)
	}
	_, err := dao.Article.Ctx(r.Context()).Where("id", id).Data(g.Map{
		"title":   title,
		"content": content,
	}).Update()
	if err != nil {
		r.Response.WriteJson(
			g.Map{
				"code": 1,
				"msg":  "更新失败: " + err.Error(),
			},
		)
		return
	}
	r.Response.WriteJson(
		g.Map{
			"code": 0,
			"msg":  "更新成功",
		},
	)
}

func Add(r *ghttp.Request) {
	title := r.Get("title").String()
	content := r.Get("content").String()
	if title == "" || content == "" {
		r.Response.WriteJson(
			g.Map{
				"code": 1,
				"msg":  "标题和内容不能为空",
			},
		)
	}
	article := &model.Article{
		Title:   title,
		Content: content,
	}
	_, err := dao.Article.Ctx(r.Context()).Data(article).Insert()
	if err != nil {
		r.Response.WriteJson(
			g.Map{
				"code": 1,
				"msg":  "添加失败: " + err.Error(),
			},
		)
		return
	}
	r.Response.WriteJson(
		g.Map{
			"code": 0,
			"msg":  "添加成功",
		},
	)

}

func Chat(r *ghttp.Request) {
	r.Response.WriteTpl("ws.html")
}
